package com.xmtlxx.web.controller.wms.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.core.validate.EditGroup;
import com.xmtlxx.common.core.validate.QueryGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.oms.domain.bo.ContactsBo;
import com.xmtlxx.oms.domain.vo.ContactsVo;
import com.xmtlxx.oms.service.IContactsService;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.service.ISysOssService;
import com.xmtlxx.web.controller.wms.service.IWarehouseService;
import com.xmtlxx.web.controller.wms.service.SmartCabinetService;
import com.xmtlxx.wms.domain.bo.VerifyOpenAllCabinetBo;
import com.xmtlxx.wms.domain.bo.WarehouseBo;
import com.xmtlxx.wms.domain.bo.WarehouseDtlBo;
import com.xmtlxx.wms.domain.vo.WarehouseVo;
import com.xmtlxx.wms.service.IWarehouseDtlService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 入库单Controller
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Validated
@Api(value = "入库单控制器", tags = {"入库单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/warehouse")
public class WarehouseController extends BaseController {

    private final IWarehouseService iWarehouseService;

    private final IWarehouseDtlService iWarehouseDtlService;

    private final SmartCabinetService smartCabinetService;

    private final IContactsService iContactsService;

    private final ISysOssService iSysOssService;

    /**
     * 返回一个初始化的对象到前端，方便进行动态渲染
     */
    @ApiOperation("用于返回一个初始化对象")
    @PostMapping("/initBean")
    R<WarehouseVo> initBean() {

        WarehouseVo vo = new WarehouseVo();
        vo.setDeptId(LoginHelper.getDeptId());

        ContactsVo contactsVo = null;
        ContactsBo contactsBo = new ContactsBo();
        contactsBo.setDeptId(LoginHelper.getDeptId());
        List<ContactsVo> contactsVos = iContactsService.queryList(contactsBo);
        //获取联系人信息
        if (contactsVos != null && contactsVos.size() > 0) {
            for (ContactsVo contacts : contactsVos) {
                if (StringUtils.isNotEmpty(contacts.getFlag()) && contacts.getFlag().equals("Y")) {
                    contactsVo = contacts;
                    break;
                }
            }
            if (ObjectUtil.isNull(contactsVo)) {
                contactsVo = contactsVos.get(0);
            }
        }
        if (ObjectUtil.isNotNull(contactsVo)) {
            vo.setName(contactsVo.getName());
            vo.setTel(contactsVo.getTel());
            vo.setAddress(contactsVo.getAddress());
        }
        //可以对数据进行初始化
        return R.ok(vo);
    }

    /**
     * 查询入库单列表
     */
    @ApiOperation("查询入库单列表")
//    @SaCheckPermission("wms:warehouse:list")
    @GetMapping("/list")
    public TableDataInfo<WarehouseVo> list(@Validated(QueryGroup.class) WarehouseBo bo, PageQuery pageQuery) {

        return iWarehouseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出入库单列表
     */
    @ApiOperation("导出入库单列表")
    @SaCheckPermission("wms:warehouse:export")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Validated WarehouseBo bo, HttpServletResponse response) {

        List<WarehouseVo> list = iWarehouseService.queryList(bo);
        ExcelUtil.exportExcel(list, "入库单", WarehouseVo.class, response);
    }

    /**
     * 获取入库单详细信息
     */
    @ApiOperation("获取入库单详细信息")
//    @SaCheckPermission("wms:warehouse:query")
    @GetMapping("/{id}")
    public R<WarehouseVo> getInfo(@ApiParam("主键")
                                  @NotNull(message = "主键不能为空")
                                  @PathVariable("id") String id) {

        WarehouseVo vo = iWarehouseService.queryById(id);
        WarehouseDtlBo bo = new WarehouseDtlBo();
        bo.setWid(id);
        bo.setDelFlag("0");
        vo.setItems(iWarehouseDtlService.queryList(bo));
        SysOssBo oss = new SysOssBo();
        oss.setObjid(vo.getId());
        oss.setBuss("wms_warehouse");
        vo.setOss(iSysOssService.selectSysOssList(oss));
        return R.ok(vo);
    }

    /**
     * 新增入库单
     */
    @ApiOperation("新增入库单")
    @SaCheckPermission("wms:warehouse:add")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WarehouseVo vo) {

        WarehouseBo bo = new WarehouseBo();
        BeanUtil.copyProperties(vo, bo);
        iWarehouseService.insertByBo(bo);
        return toAjax(1);
    }

    /**
     * 修改入库单
     */
    @ApiOperation("修改入库单")
    @SaCheckPermission("wms:warehouse:edit")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    @Transactional(rollbackFor = Exception.class)
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WarehouseVo vo) {

        WarehouseBo bo = new WarehouseBo();
        BeanUtil.copyProperties(vo, bo);
        iWarehouseService.updateByBo(bo);
        return toAjax(1);
    }

    /**
     * 删除入库单
     */
    @ApiOperation("删除入库单")
    @SaCheckPermission("wms:warehouse:remove")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> remove(@ApiParam("主键串")
                          @NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {

        return toAjax(iWarehouseService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }


    @ApiOperation("获取验证一键开柜")
    @GetMapping("verifyOpenAllCabinet/page")
    public TableDataInfo<VerifyOpenAllCabinetBo> getVerifyOpenAllCabinet(PageQuery pageQuery) {

        return smartCabinetService.getVerifyOpenAllCabinet(pageQuery);
    }

}
